package com.zhl.emp_manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhl.emp_manage.entity.Meeting;
import com.zhl.emp_manage.service.MeetingService;
import com.zhl.emp_manage.utils.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "会议管理")
@RestController
@RequestMapping("/meeting")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;

    /**
     * 查看所有会议
     *
     * @return
     */
    @ApiOperation("查看所有会议")
    @GetMapping("/meetings")
    public Results getList() {
        List<Meeting> list = meetingService.getList();
        return Results.ok().data("list", list);
    }

    /**
     * 根据id查询会议
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询会议")
    @GetMapping("/meetings/{id}")
    public Results getOne(@PathVariable Integer id) {
        Meeting meeting = meetingService.getOne(id);
        return Results.ok().data("meeting", meeting);
    }

    /**
     * 添加会议
     *
     * @param meeting
     * @return
     */
    @ApiOperation("添加会议")
    @PostMapping("/meetings")
    public Results addMeeting(@RequestBody Meeting meeting) {
        boolean b = meetingService.addMeeting(meeting);
        return Results.ok().message("添加成功");
    }

    /**
     * 根据id删除会议
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除会议")
    @DeleteMapping("/meetings/{id}")
    public Results deleteMeeting(@PathVariable Integer id) {
        boolean b = meetingService.deleteMeeting(id);
        return Results.ok().message("删除成功");
    }

    /**
     * 会议名称模糊查询
     *
     * @param name
     * @return
     */
    @ApiOperation("会议名称模糊查询")
    @GetMapping("/meetings/meetingName")
    public Results getListLike(@RequestParam(value = "name") String name) {
        List<Meeting> listLike = meetingService.getListLike(name);
        return Results.ok().data("listLike", listLike);
    }

    /**
     * 修改会议
     *
     * @param meeting
     * @return
     */
    @ApiOperation("修改会议")
    @PostMapping("/meetings/{id}")
    public Results updateMeeting(@RequestBody Meeting meeting, @PathVariable Integer id) {
//        Date meetingCreatetime = meeting.getMeetingCreatetime();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = simpleDateFormat.format(meetingCreatetime);
//        try {
//            meeting.setMeetingCreatetime(simpleDateFormat.parse(format));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        boolean b = meetingService.updateMeeting(meeting);
        return Results.ok().message("修改成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/meetings/pages")
    public Results getMeetingMore(@RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Meeting> meetingMore = meetingService.getMeetingMore(page, limit);
        PageInfo<Meeting> pageInfo = new PageInfo<>(meetingMore);
        return Results.ok().data("pageInfo", pageInfo);
    }
}
